<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
<!--        <BasicUpload :maxSize="20" :maxNumber="1" @change="handleChange" :api="handleUpdateApi" />-->
        <JinhuiImpExcel
          module="interface_monthly_data_maintenance"
          businessType="import"
          importUrl="/ttOrderCancelRecord/importttOrderCancelRecord.html"
          @success="reload"
        >
          <a-button style="margin-right: 4px;" preIcon="ant-design:file-add-outlined">
            导入
          </a-button>
        </JinhuiImpExcel>
        <a-button preIcon="ant-design:download-outlined" @click="handleExportExcel">
          导出
        </a-button>
      </template>
    </BasicTable>
    <tmUserModal @register="registerModal" @success="reload" />
  </div>
</template>

<script lang="ts" name="firstUpperEntityName" setup>
  import { BasicTable } from '/@/components/Table';
  import { useJinhuiTable } from '/@/components/Jinhui/hooks/useJinhuiTable';
  import { getPage } from './orderCancel.api';
  import { columns, dates, searchFormSchema } from './orderCancel.data';
  import { useModal } from '/@/components/Modal';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downFile, uploadFile } from '/@/api/sys/file';
  import {JinhuiImpExcel} from '/@/components/Excel';
  import {handleExportExcelData} from "/@/components/Excel/src/JinhuiExportExcel";

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, getForm, clearSelectedRowKeys }, { rowSelection }] =
    useJinhuiTable({
      api: getPage,
      columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      showActionColumn: false,
      useSearchForm: true,
      //组件加载后是否立即请求接口
      immediate: false,
      tableSetting:{
        // 是否显示刷新按钮
        redo: false
      },
      beforeFetch: (params) => {
        //根据页数计算start
        let start;
        if (params.start == 1) {
          start = 0;
        } else {
          start = (params.start - 1) * params.limit;
        }
        return {
          'queryData.orderId': params.orderId,
          'queryData.reportDateBegin': dates[0],
          'queryData.reportDateEnd': dates[1],
          'queryData.start': start,
          'queryData.limit': params.limit,
          ...params,
        };
      },
    });

  const handleUpdateApi = (file, cb) => {
    return uploadFile('/upload/ttOrderCancelRecord/importttOrderCancelRecord.html', file, cb);
  };
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleChange(list: string[]) {
    createMessage.info(`已上传文件${JSON.stringify(list)}`);
  }

  /**
   * 修改按钮
   * @param record
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function refresh() {
    Promise.resolve(clearSelectedRowKeys()).then(() => {
      reload();
    });
  }

  async function handleExportExcel() {
    const query = await getForm().validate();
    Object.assign(query, {
      'queryData.reportDateBegin': query.dateTime[0],
      'queryData.reportDateEnd': query.dateTime[1],
      'queryData.orderId': query.orderId,
    });

    //导出路径
    const exportDataUrl='/ttOrderCancelRecord/exportttOrderCancelRecord.html'
    //下载路径
    const downLoadUrl='/ttOrderCancelRecord/downloadttOrderCancelRecordFile.html?id='
    //导出
    handleExportExcelData(query,exportDataUrl,downLoadUrl);

    // await defHttp
    //   .request({
    //     url: '/ttOrderCancelRecord/exportttOrderCancelRecord.html',
    //     params: query,
    //     method: 'get',
    //     timeout: 300000,
    //   })
    //   .then((res) => {
    //     var obj = eval('(' + res + ')');
    //     if (obj.success === true) {
    //       var fileName = obj.data.substring(obj.data.lastIndexOf('/') + 1, obj.data.length);
    //       var exportDataUrl =
    //         '/ttOrderCancelRecord/downloadttOrderCancelRecordFile.html?id=' + obj.data;
    //       downFile(exportDataUrl).then((data) => {
    //         if (!data) {
    //           createMessage.warning('文件导出失败,请重新下载');
    //           return;
    //         }
    //         if (data.size == 0) {
    //           createMessage.warning('文件导出异常,请稍后重新下载');
    //           return;
    //         }
    //         const nav = window.navigator as any;
    //         if (typeof nav.msSaveBlob !== 'undefined') {
    //           nav.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName);
    //         } else {
    //           const url = window.URL.createObjectURL(
    //             new Blob([data], { type: 'application/vnd.ms-excel' }),
    //           );
    //           const link = document.createElement('a');
    //           link.style.display = 'none';
    //           link.href = url;
    //           link.setAttribute('download', fileName);
    //           document.body.appendChild(link);
    //           link.click();
    //           document.body.removeChild(link); //下载完成移除元素
    //           window.URL.revokeObjectURL(url); //释放掉blob对象
    //         }
    //         //清除loading
    //         createMessage.destroy();
    //       });
    //     }
    //   });
  }
</script>
